package deom1;

public class Test1 {

}


public class TreeNode {
     int val;
    TreeNode left;
     TreeNode() {}
     TreeNode(int val, TreeNode left, TreeNode right) {
        this.left = left;
        this.right = right;
     }
 }
class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        return (Math.abs(leftHeight - rightHeight) <= 1) && isBalanced(root.left) && isBalanced(root.right);


    }
    public int getTreeHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);

        return Math.max(leftHeight,rightHeight) + 1;
    }
}

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        TreeNode ret = root.right;
        root.right = root.left;
        root.left = ret;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
